<?php

class          Common_DataTable_Model_DatabaseTable 
    extends    Common_DataTable_Model_Abstract 
    implements Common_DataTable_Model_SelfDefining,
               Common_DataTable_Model_Sortable,
               Common_DataTable_Model_Groupable
{
    
    private $str_table = null;
    private $obj_datagateway = null;
    
    public function __construct(Common_DataGateway_Database $obj_datagateway, $str_table) {
        parent::__construct();
        
        $this->__addMixin(new Common_DataTable_Model_Mixin_SortMeta(), 'sort');
        $this->__addMixin(new Common_DataTable_Model_Mixin_GroupMeta(), 'group');
        $this->__addMixin(new Common_DataTable_Model_Mixin_HeaderDetector(), 'detector');
        
        $this->setDataGateway($obj_datagateway);
        $this->setTableName($str_table);        
    }
    
    protected function setDataGateway(Common_DataGateway_Database $obj_datagateway) {
        $this->obj_datagateway = $obj_datagateway;
    }
    
    protected function getDataGateway() {
        return $this->obj_datagateway;
    }
    
    public function setTableName($str_table_name) {
        if(is_string($str_table_name)) {
            $this->str_table = $str_table_name;
        }
    }
    
    protected function getTableName() {
        return $this->str_table;
    }
    
    public function getData($int_size = null, $int_offset = null) {
        
        $obj_select = $this->obj_datagateway->select();
        
        $obj_select->from($this->getTableName());
        
        if($this->sort__hasSortMeta()) {
            
            $arr_sort_meta = $this->sort__getSortMeta();
            
            $arr_sorting = array();
            foreach($arr_sort_meta as $str_sort_key => $arr_meta) {
                
                $enm_direction = $arr_meta['direction'];
                $str_direction = ($enm_direction == SORT_ASC) ? 'ASC' : 'DESC';
                
                $arr_sorting[] = $str_sort_key . ' ' . $str_direction;
            }
            
            $obj_select->order($arr_sorting);
        }
        
        if(!is_null($int_size) && !is_null($int_offset)) {
            $obj_select->limit($int_size, $int_offset);
        }

        $obj_results = $this->obj_datagateway->execute_select($obj_select);

        $int_data_count = $obj_results->rowCount();
        $arr_data = $this->group__generateDataGroups($obj_results);
        
        $arr_output = array_merge(array(
            'count' => $int_data_count
        ), $arr_data);
        
        return $arr_output;
    }

    public function hasData() {
        
        $sql = 'SELECT COUNT(1) FROM ' . $this->getTableName();

        $obj_results = $this->obj_datagateway->execute_query($sql);
        $int_count = $obj_results->fetchColumn();
        
        return ($int_count > 0);
    }
    
    public function sortDataBy($arr_sort_meta) {
        $this->sort__sortDataBy($arr_sort_meta);
    }
    
    public function groupDataBy($arr_group_meta) {
        $this->group__groupDataBy($arr_group_meta);
    }
    
    public function defineHeaders() {
        
        $arr_headers = null;
        
        $obj_select = $this->obj_datagateway->select();
        
        $obj_select->from($this->getTableName());
        
        $obj_select->limit(1);
        
        $obj_results = $this->obj_datagateway->execute_select($obj_select);
        $arr_results = $obj_results->fetchAll();
        
        if(!empty($arr_results)) {
            
            $arr_first = reset($arr_results);
            
            if(is_array($arr_first)) {
                $arr_headers = $this->detectHeaders($arr_first);
            }
            
        }
        
        return $arr_headers;
    }
    
}
